﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Threading;
using NotifierService;
using System.ServiceModel.Description;
using NotifierService;
using CMD = System.Console;

namespace NotifierService.Console
{
    class Program
    {
        static void Main(string[] args)
        {

            //The ip address of server
            Uri tcpUri = new Uri("net.tcp://192.168.1.156:8001/notifier");
            //Uri httpUri = new Uri("http://localhost:8002");

            CMD.WriteLine("Start Notifier service...");
            var host = new ServiceHost(typeof(Service.EasyService), tcpUri);

            var netTcpBinding = new NetTcpBinding();
            
            netTcpBinding.ReliableSession.Enabled = true;
            netTcpBinding.ReliableSession.InactivityTimeout = TimeSpan.FromMinutes(25);
            netTcpBinding.Security.Mode = SecurityMode.Transport;

            //service.
            host.Description.Endpoints.Clear();
            host.Description.Behaviors.Find<ServiceMetadataBehavior>().HttpGetEnabled = false;
            host.AddServiceEndpoint(typeof(Service.IServiceBase), netTcpBinding, tcpUri);

            

            host.Open();

            CMD.WriteLine("Service started.");

            CMD.WriteLine("Notifier Service v1.0.0\n=====================");

            try
            {
                Service.RealtimeBroadcastServiceV4.GetInstance().Start();
            } catch (Service.RealtimeBroadcastServiceV4.IPv4NotSupportException) { }

            try
            {
                Service.RealtimeBroadcastServiceV6.GetInstance().Start();
            }
            catch (Service.RealtimeBroadcastServiceV6.IPv6NotSupportException) { }

            while (host.State == CommunicationState.Opened)
            {
                Thread.Sleep(1000);
            }
        }
    }
}
